{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9b827b72",
   "metadata": {},
   "source": [
    "# 实践 face++ 人脸检测/face detect\n",
    "### API实践步骤\n",
    "1. 导入需要的requests模块\n",
    "\n",
    "2. 输入我们需要API网站注册的API_Key\n",
    "\n",
    "3. 目标url\n",
    "\n",
    "4. 沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "\n",
    "5. 准备symbol ? 后面的数据,这里需要注意,一定要详细阅读API文档中的 “参数功能”,按照要求格式准备payload参数功能可能有:\n",
    " >* 是否必要?必要的一定要准备好 \n",
    " >* 选填的一定是功能,要根据功能需求 好好填噢\n",
    " \n",
    "6. response响应  .注意:\n",
    " >* 详细阅读文档,注意请求方式(GET、POST、DELETE) \n",
    " >* 注意json 和字典的差异 ,str vs dict"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e314178a",
   "metadata": {},
   "source": [
    "## 准备步骤\n",
    "### 人脸检测API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "99abc223",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.导入需要的requests模块\n",
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "f0a4ad03",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.输入我们需要api网站注册的API_Key\n",
    "API_key = \"PLnBSTEIGPSyN2SxwRwU-0R1jDQSaiPL\"\n",
    "API_secret = \"u1r0HidAt7A9TEI_QOgt-rRKPMERdls1\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "7ed583b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3.准备api测试接口的url\n",
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/detect\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "6567a220",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4.调用方法post\n",
    "# 5.准备url传递参数\n",
    "payload = {\n",
    "    \"api_key\":API_key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"image_url\":\"https://img2.baidu.com/it/u=3345631249,3902403825&fm=253&fmt=auto&app=138&f=JPEG?w=499&h=303\",\n",
    "    \"return_attributes\":\"gender,age,beauty\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3734e585",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648495591,205cc429-9f10-4ad0-bb6c-de1806374c2b',\n",
       " 'time_used': 203,\n",
       " 'faces': [{'face_token': '3d07b87bd1a3460086c64e7e1549f089',\n",
       "   'face_rectangle': {'top': 94, 'left': 197, 'width': 144, 'height': 144},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 20},\n",
       "    'beauty': {'male_score': 76.93, 'female_score': 80.032}}}],\n",
       " 'image_id': 'bxyX2kMs83UoebusOwispw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 6.发起请求\n",
    "r = requests.post(url=face_detect,params=payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "4b23dd42",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_detect = r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e8e46d72",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_tokens = face_detect['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "de0306d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3d07b87bd1a3460086c64e7e1549f089'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_tokens"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c10e6744",
   "metadata": {},
   "source": [
    "### 人脸分析API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "ab0cf7a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 214,\n",
       " 'request_id': '1648496376,640cedd5-5fb1-4d7e-b173-9950430aeced',\n",
       " 'faces': [{'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 20},\n",
       "    'headpose': {'yaw_angle': 3.3268852,\n",
       "     'pitch_angle': 4.55614,\n",
       "     'roll_angle': 0.71018577},\n",
       "    'smile': {'threshold': 50.0, 'value': 0.029}},\n",
       "   'face_rectangle': {'width': 144, 'top': 94, 'left': 197, 'height': 144},\n",
       "   'face_token': '3d07b87bd1a3460086c64e7e1549f089'}]}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. url\n",
    "Analyze_base_url  = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "# 2. api账户/通行证\n",
    "API_key = \"PLnBSTEIGPSyN2SxwRwU-0R1jDQSaiPL\"\n",
    "API_secret = \"u1r0HidAt7A9TEI_QOgt-rRKPMERdls1\"\n",
    "# 3. POST\n",
    "# 4. 所有 API Key 都可以调用本 API。\n",
    "#    其中 calculate_all 和 face_rectangle 参数只有正式 API Key 才能使用，试用 API Key 无法使用。\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'face_tokens':face_tokens,\n",
    "    'return_attributes':'gender,age,smiling,headpose'\n",
    "}\n",
    "\n",
    "r_analyze  = requests.post(Analyze_base_url, params = payload)\n",
    "r_analyze\n",
    "r_analyze.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "da884a0a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>3d07b87bd1a3460086c64e7e1549f089</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.age.value</th>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.yaw_angle</th>\n",
       "      <td>3.326885</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.pitch_angle</th>\n",
       "      <td>4.55614</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.roll_angle</th>\n",
       "      <td>0.710186</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <td>0.029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>144</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>197</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>144</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                0\n",
       "face_token                       3d07b87bd1a3460086c64e7e1549f089\n",
       "attributes.gender.value                                      Male\n",
       "attributes.age.value                                           20\n",
       "attributes.headpose.yaw_angle                            3.326885\n",
       "attributes.headpose.pitch_angle                           4.55614\n",
       "attributes.headpose.roll_angle                           0.710186\n",
       "attributes.smile.threshold                                   50.0\n",
       "attributes.smile.value                                      0.029\n",
       "face_rectangle.width                                          144\n",
       "face_rectangle.top                                             94\n",
       "face_rectangle.left                                           197\n",
       "face_rectangle.height                                         144"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "pd.json_normalize(r_analyze.json()['faces']).T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5489607",
   "metadata": {},
   "source": [
    "### 人脸对比API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "d23394a7",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'API_Key' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-36-0dba7bf6c726>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m payload = {\n\u001b[1;32m----> 7\u001b[1;33m     \u001b[1;34m'api_key'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mAPI_Key\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      8\u001b[0m     \u001b[1;34m'api_secret'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mAPI_secret\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m     \u001b[1;34m'image_url1'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg2.ixinwei.com%2F202104%2F7848.jpg&refer=http%3A%2F%2Fimg2.ixinwei.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651088081&t=b1a3fffbf966a81ed9c9a8eab664975d'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'API_Key' is not defined"
     ]
    }
   ],
   "source": [
    "compare_base_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "API_key = \"PLnBSTEIGPSyN2SxwRwU-0R1jDQSaiPL\"\n",
    "API_secret = \"u1r0HidAt7A9TEI_QOgt-rRKPMERdls1\"\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_Key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url1':'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg2.ixinwei.com%2F202104%2F7848.jpg&refer=http%3A%2F%2Fimg2.ixinwei.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651088081&t=b1a3fffbf966a81ed9c9a8eab664975d',\n",
    "    'image_url2\t':'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F13230169866%2F1000.jpg&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651088270&t=1099097c11937b7c105eb599cb7586be'\n",
    "}\n",
    "\n",
    "r_compare = requests.post(compare_base_url, params = payload)\n",
    "r_compare\n",
    "r_compare.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a08315b2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
